<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * 
 */
 
require_once(ROOT_PATH."class/user.class.php");
require_once(ROOT_PATH."class/team.class.php");

require_once (ROOT_PATH."xajax_core/xajax.inc.php");

$xajax = new xajax("xr.php");
$xajax->registerFunction("sendMessage");
$tm->assign('script',$xajax->getJavascript('scripts'));

$team = new team;
$team->status=STATUS_ACTIVE;

if (isset($CURRENT_USER) && $CURRENT_USER->groupid == GROUP_COORD) {
	$team->id=$CURRENT_USER->teamid;
	$loader->get($team);
	$list = array($team);	
} else {
	$list = $loader->loadListBy($team,$team->listByStatus());	
}

$tm->assign('timezonelist',$TIMEZONE_LIST);
$tm->assign('langlist',$LANGUAGE_LIST);
$tm->assign('teamlist',$list);
$tm->assign('param',$param);

if ($param['op']=='add' and $flow_ok) {
	if (isEmpty($param['userid'])
		or
		isEmpty($param['mail'])
		or
		isEmpty($param['remail'])
		or
		/*isEmpty($param['password'])
		or
		isEmpty($param['repassword'])
		or*/
		isEmpty($param['lang'])
		or
		(isEmpty($param['team']) && $CURRENT_USER->groupid != GROUP_ADMIN)
		or
		isEmpty($param['timezone'])
		) {
		$error = $LANG['regmissing'];
		$tm->assign('error',$error);		
		$content = $tm->fetch('reguser.tpl');
		/*
	} elseif ($param['password']!=$param['repassword']) {
		$error = $LANG['passnotmatch'];
		$content = $tm->fetch('reguser.tpl');
		*/
	} elseif ($param['mail']!=$param['remail']) {
		$error = $LANG['mailnotmatch'];
		$content = $tm->fetch('reguser.tpl');
	} elseif (!isValidEmail($param['mail'])) {
		$error = $LANG['invalidmail'];
		$content = $tm->fetch('reguser.tpl');
	} elseif (strlen($param['userid'])<MIN_USER_LEN) {
		$error = $LANG['useridtooshort'];
		$content = $tm->fetch('reguser.tpl');
/*
	} elseif (strlen($param['password'])<MIN_PASSWORD_LEN) {
		$error = $LANG['passtooshort'];
		$content = $tm->fetch('reguser.tpl');*/
	} else {
		$us = new user;
		$us->userid=$param['userid'];
		$ret = $loader->get($us);
		if ($ret) {
			$error = $LANG['userexists'];
			$content = $tm->fetch('reguser.tpl');
		} else {
			$pwd = util::hashcode(util::createPassword(10));
			
			$us->mail=$param['mail'];
			//$us->password=$param['password'];
			$us->password=$pwd;
			$us->lang=$param['lang'];
			$us->teamid=$param['team'];
			$us->groupid=GROUP_USER;		
			$us->timezone=$param['timezone'];
			$us->status=INITIAL_USER_STATE;
			
			$us->passcode = $pwd;
			$us->passexpire = util::now();			

			$ret = $loader->save($us);

			updateLang($us->lang);

			sendTeamMail($us);
			sendConfirmMail($us,$pwd);
		
			if (INITIAL_USER_STATE == STATUS_ACTIVE) {
				regReceived($us,'regreceived');
				$message = $LANG['regcomplete'];
			} else {
				regReceived($us,'regreceivedinactive');
				$message = $LANG['regcompleteinactive'];
			}
			$content = createMessage($homelink,array($LANG['home']));
		}
	}
} else {
	$content = $tm->fetch('reguser.tpl');
}

?>
